<template>
  <div
    v-show="show"
    class="questionnaireItem question-item-wrap"
    @mouseenter="contorlShow=true"
    @mouseleave="contorlShow=false"
  >
    <div class="topic">
      <Controls
        v-if="type==='0'"
        :item="JSON.parse(JSON.stringify(formData))"
        :show="contorlShow"
        :is-score="formData.isScore"
        :list="list"
        @refresh="$emit('refresh')"
      ></Controls>
      <div>
        <!-- 编辑模式 -->
        <div v-if="type==='0'">
          <p
            class="subject_title"
            :style="formData.c_index?'font-size:1rem':'font-size:1.1rem'"
          >{{ formData.topic_index+(formData.c_index?('.'+formData.c_index):'')+'、 '+formData.questionTitle+(formData.totalScore?'('+formData.totalScore+'分)':'') }}</p>
          <div class="answer">
            <el-radio-group v-if="formData.questionType===0" v-model="radio">
              <div
                v-for="(option,index) in formData.questionOptionDTOList"
                :key="index"
                class="answer_option edit_option"
              >
                <span class="answer_option_index">{{ transNum(index) }}</span>
                <el-radio :label="option.id">{{ option.optionContent }}</el-radio>
                <el-input
                  v-if="option.isEnter"
                  v-model="option.enterContent"
                  type="textarea"
                  :autosize="{minRows:1}"
                  class="enterContent_edit"
                ></el-input>
              </div>
            </el-radio-group>
            <el-checkbox-group v-if="formData.questionType===1" v-model="checkList">
              <div
                v-for="(option,index) in formData.questionOptionDTOList"
                :key="index"
                class="answer_option edit_option"
              >
                <span class="answer_option_index">{{ transNum(index) }}</span>
                <el-checkbox :label="option.id">{{ option.optionContent }}</el-checkbox>
                <el-input
                  v-if="option.isEnter"
                  v-model="option.enterContent"
                  type="textarea"
                  :autosize="{minRows:1}"
                  class="enterContent_edit"
                ></el-input>
              </div>
            </el-checkbox-group>
            <div v-if="formData.questionType===2">
              <div
                v-for="(option,index) in formData.questionOptionDTOList"
                :key="index"
                class="answer_option"
              >
                <div style="flex-grow:1">
                  <p
                    v-if="option.optionContent"
                    class="optionContent_title"
                  >{{ option.optionContent }}</p>
                  <el-input v-model="option.enterContent" type="textarea" :autosize="{minRows:1}"></el-input>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div v-else>
          <div class="subject_title" :style="formData.c_index?'font-size:1rem':'font-size:1.1rem'">
            <p class="subject_title_flex_box">
              <span
                class="topicNumber"
              >{{ formData.topic_index+(formData.c_index?('.'+formData.c_index):'') }}</span>
              <span>{{ formData.questionTitle }}{{ formData.totalScore?('('+formData.totalScore+'分)'):'' }}</span>
            </p>
          </div>
          <div v-if="formData.questionType===0">
            <div v-if="type==='3'||type==='4'" class="evaluate_title">
              <i></i>
              <p>质检评语</p>
              <p v-if="type==='4'">申诉备注</p>
            </div>
            <div
              v-for="(option,index) in formData.questionOptionDTOList"
              :key="index"
              :class="type!=='1'?'no_previews answer_option':'answer_option'"
            >
              <div
                :class="index===0?'answer_option_child topBorder':'answer_option_child'"
                @click="selectedOption(option)"
              >
                <span
                  class="option_radio item_index"
                  :style="itemAnswer[option.id].isSel?'background: #16BC83;color: #fff':''"
                >{{ transNum(index) }}</span>
                <p>{{ option.optionContent }}</p>
                <el-input
                  v-if="option.isEnter"
                  v-model="itemAnswer[option.id].enterContent"
                  class="enterContent_edit"
                  type="textarea"
                  :autosize="{minRows:1}"
                ></el-input>
              </div>
              <!-- 质检评语 -->
              <div v-if="type==='3'||type==='4'" class="answer_option_evaluate">
                <el-input v-model="itemAnswer[option.id].evaluate" type="textarea" :autosize="{minRows:1}" :disabled="type==='4'"></el-input>
              </div>
              <!-- 申诉备注 -->
              <div v-if="type==='4'" class="answer_option_evaluate">
                <el-input v-model="itemAnswer[option.id].appeal" type="textarea" :autosize="{minRows:1}"></el-input>
              </div>
            </div>
          </div>
          <div v-if="formData.questionType===1">
            <div v-if="type==='3'||type==='4'" class="evaluate_title">
              <i></i>
              <p>质检评语</p>
              <p v-if="type==='4'">申诉备注</p>
            </div>
            <div
              v-for="(option,index) in formData.questionOptionDTOList"
              :key="index"
              :class="type!=='1'?'no_previews answer_option':'answer_option'"
            >
              <div
                :class="index===0?'answer_option_child topBorder':'answer_option_child'"
                @click="selectedOption(option)"
              >
                <span
                  class="option_checkbox item_index"
                  :style="itemAnswer[option.id].isSel?'background: #16BC83;color: #fff':''"
                >{{ transNum(index) }}</span>
                <p>{{ option.optionContent }}</p>
                <el-input
                  v-if="option.isEnter"
                  v-model="itemAnswer[option.id].enterContent"
                  type="textarea"
                  :autosize="{minRows:1}"
                  class="enterContent_edit"
                ></el-input>
              </div>
              <!-- 质检评语 -->
              <div v-if="type==='3'||type==='4'" class="answer_option_evaluate">
                <el-input v-model="itemAnswer[option.id].evaluate" type="textarea" :autosize="{minRows:1}" :disabled="type==='4'"></el-input>
              </div>
              <!-- 申诉备注 -->
              <div v-if="type==='4'" class="answer_option_evaluate">
                <el-input v-model="itemAnswer[option.id].appeal" type="textarea" :autosize="{minRows:1}"></el-input>
              </div>
            </div>
          </div>
          <div v-if="formData.questionType===2">
            <div v-if="type==='3'||type==='4'" class="evaluate_title">
              <i></i>
              <p>质检评语</p>
              <p v-if="type==='4'">申诉备注</p>
            </div>
            <div
              v-for="(option,index) in formData.questionOptionDTOList"
              :key="index"
              :class="type!=='1'?'no_previews answer_option':'answer_option'"
            >
              <div
                :class="index===0?'answer_option_child topBorder':'answer_option_child'"
              >
                <div style="flex-grow:1">
                  <p
                    v-if="option.optionContent"
                    class="optionContent_title"
                    style="margin:0 0 5px"
                  >{{ option.optionContent }}</p>
                  <el-input v-model="itemAnswer[option.id].enterContent" type="textarea" :autosize="{minRows:1}" style="width:100%"></el-input>
                </div>
              </div>
              <!-- 质检评语 -->
              <div v-if="type==='3'||type==='4'" class="answer_option_evaluate">
                <el-input v-model="itemAnswer[option.id].evaluate" type="textarea" :autosize="{minRows:1}" :disabled="type==='4'"></el-input>
              </div>
              <!-- 申诉备注 -->
              <div v-if="type==='4'" class="answer_option_evaluate">
                <el-input v-model="itemAnswer[option.id].appeal" type="textarea" :autosize="{minRows:1}"></el-input>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Controls from '../Controls'
export default {
  name: 'QuestionnaireItem',
  components: {
    Controls
  },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    list: {
      type: Array,
      default: () => {
        return []
      }
    },
    type: {
      type: String,
      default: '0' // 0为编辑模式式，1为预览模式，2为答题模式，3为质检模式，4为申诉模式
    },
    answer: {
      // 问卷答案
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      radio: '',
      contorlShow: false,
      checkList: [],
      formData: {},
      itemAnswer: {
        // 本题答案
        id: {
          isSel: false, // 是否选中
          enterContent: '', // 当前项输入内容
          evaluate: '', // 评语
          appeal: '' // 申诉备注
        } // 选中的选项
      }, // 本题答案
      formRules: {},
      listAry: []
    }
  },
  computed: {
    show() {
      // 判断题目前置条件，决定是否显示
      var bol = true
      if (this.type === '0' || this.type === '1') {
        bol = true
      } else {
        if (this.item.prefixQuestionId) {
          bol = false
          if (
            this.answer[this.item.prefixQuestionId][this.item.prefixOptionId]
              .isSel
          ) {
            bol = true
          }
        }
      }
      return bol
    }
  },
  watch: {
    item: {
      handler(val) {
        var list = val.questionOptionDTOList
        list.forEach(e => {
          e.comment = ''
          e.appealRemark = ''
        })
        val.questionOptionDTOList = list
        this.formData = val
      },
      deep: true,
      immediate: true
    },
    answer: {
      handler(val) {
        this.itemAnswer = val[this.formData.id]
      },
      deep: true,
      immediate: true
    },
    itemAnswer: {
      handler(val) {
        if (this.type === '1') {
          return
        }
        // 本题答案更改，更新全部答案对象
        var obj = this.answer
        obj[this.formData.id] = val
        this.$emit('change', obj)
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    transNum(num) {
      var ary = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
      return ary[num]
    },
    // 选择
    selectedOption(option, ev) {
      // 预览模式不能选择
      if (this.type === '1') {
        return
      }
      if (this.formData.questionType === 0) {
        this.itemAnswer[option.id].isSel = true
        for (var i in this.itemAnswer) {
          if (i !== option.id) {
            this.itemAnswer[i].isSel = false
          }
        }
      } else {
        this.itemAnswer[option.id].isSel = !this.itemAnswer[option.id].isSel
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.questionnaireItem{
  padding:0 20px
}
.chapter,
.topic {
  border-top: 1px dashed #ccc;
  position: relative;
}
.topic {
  padding: 20px 20px;
}
.answer_option {
  margin-left: 40px;
  display: flex;
  align-items: center;
  .answer_option_child {
    display: flex;
    align-items: center;
    flex-grow: 1;
    padding: 5px 40px;
    min-height: 65px;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-top: none;
  }
  .topBorder{
    border-top: 1px solid #ccc;
  }
  .answer_option_evaluate {
    width: 20%;
    padding: 0 20px;
  }
}
.answer_option_child:hover{
  cursor: pointer;
  // background: #ccc;
  .item_index {
    // background: #16BC83;
    // color: #fff;
  }
}
.evaluate_title {
  display: flex;
  align-items: center;
  i {
    flex-grow: 1;
  }
  p {
    margin: 0;
    text-align: center;
    width: 20%;
  }
}
.topicNumber {
  display: inline-block;
  width: 32px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  border: 1px solid #16BC83;
  background: #16BC83;
  color: #fff;
  margin-right: 10px;
}
.item_index {
  display: inline-block;
  width: 32px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  border: 1px solid #999;
  background: #fff;
  color: #000;
  margin-right: 10px;
  flex-shrink:0;
}
.option_radio {
  border-radius: 16px;
}
.edit_option {
  min-height: 32px;
  box-sizing: border-box;
  padding: 5px 0;
  .answer_option_index {
    display: inline-block;
    color: #000;
    font-size: 1rem;
    margin-right: 20px;
  }
}
.enterContent_edit {
  // width: 160px;
  max-width: 200px;
  margin-left: 20px;
}
.optionContent_title {
  opacity: 0.8;
}
.subject_title {
  line-height: 1.2rem;
}
.subject_title_flex_box{
  display: flex;
  font-size: 14px;
  &>:first-child{
    flex: 0 0 auto;
  }
}
</style>
<style lang="scss">
  .question-item-wrap{
    & [class*="el-radio-group"]{
      width: 100%;
    }
    & [class*="el-radio__label"]{
      max-width: 100%;
      word-break: break-all;
      white-space: pre-wrap;
      display: inline-block;
    }
    & [class="el-radio"],[class="el-radio is-checked"],[class="el-radio is-focus is-checked"]{
      display: flex;
      align-items: center;
    }
    & [class="el-checkbox is-checked"],[class="el-checkbox"]{
      display: flex;
      align-items: flex-start;
    }
  }
</style>
